package com.jsb.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.annotation.SystemControllerLog;
import com.jsb.service.AdminService;
import com.jsb.service.SysAdminRoleService;
import com.jsb.service.SysRoleService;
import com.jsb.common.utils.IdGen;
import com.jsb.common.utils.Md5Util;
import com.jsb.common.utils.StringUtils;
import com.jsb.model.Admin;
import com.jsb.model.AdminExample;
import com.jsb.model.SysAdminRole;
import com.jsb.model.SysAdminRoleExample;
import com.jsb.model.SysAdminRoleExample.Criteria;
import com.jsb.model.SysRole;

@Controller
public class AdminController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/admin/list.do";
	
	@Resource
	private AdminService adminService;
	@Resource
	private SysRoleService sysRoleService;
	@Resource
	private SysAdminRoleService sysAdminRoleService;
	
	@RequestMapping("/admin/list")
	@RequiresRoles(value = { "admin" })
	@RequiresAuthentication
	public String list(
			Model model,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		AdminExample adminExample = new AdminExample();
		int totalCount = adminService.countAdmin(adminExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		adminExample.setPageIndex((pageNum - 1) * pageSize);
		adminExample.setPageSize(pageSize);
		List<Admin> admins = adminService.getPageAdmin(adminExample);
		for (Admin admin : admins) {
			String id = admin.getId();
			SysAdminRoleExample example = new SysAdminRoleExample();
			Criteria createCriteria = example.createCriteria();
			createCriteria.andUserIdEqualTo(id);
			String role = "";
			List<SysAdminRole> uroles = sysAdminRoleService.selectByExample(example );
			for (SysAdminRole sysAdminRole : uroles) {
				String roleId = sysAdminRole.getRoleId();
				SysRole sysRoleById = sysRoleService.getSysRoleById(roleId);
				String name = sysRoleById.getName();
				if(StringUtils.isNotEmpty(name)){
					name += ",";
					role += name;
				}
			}
			
			admin.setRoleId(role);
		}
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("admins", admins);
		return "/Admin/list";
	}
	
	@RequestMapping("/admin/toAdd")
	@RequiresRoles(value = { "admin" })
	@RequiresAuthentication
	public String toAdd(Model model) {
		List<SysRole> roles = sysRoleService.selectByExample(null);
		model.addAttribute("roles", roles);
		return "/Admin/add";
	}

	@RequestMapping("/admin/add")
	@RequiresRoles(value = { "admin" })
	@RequiresAuthentication
	@SystemControllerLog(description = "添加后台用户")
	public String add(Model model, @ModelAttribute("admin") Admin admin) {
		String id = IdGen.randomBase62(5);
		String roleId = admin.getRoleId();
		admin.setId(id);
		admin.setPassword(Md5Util.Md5("111111"));
		adminService.addAdmin(admin);
		addURole(id, roleId);
		return redirect;
	}

	@RequestMapping("/admin/del")
	@RequiresRoles(value = { "admin" })
	@RequiresAuthentication
	@SystemControllerLog(description = "删除后台用户")
	public String del(Model model,
			@RequestParam(value = "id", required = true) String id) {
		adminService.delAdminById(id);
		List<SysAdminRole> uroles = getURoles(id);
		delURoles(uroles);
		return redirect;
	}

	@RequestMapping("/admin/toEdit")
	@RequiresRoles(value = { "admin" })
	@RequiresAuthentication
	public String toEdit(Model model,
			@RequestParam(value = "id", required = true) String id) {
		Admin admin = adminService.getAdminById(id);
		model.addAttribute("admin", admin);
		List<SysAdminRole> uRoles = getURoles(id);
		String roleId = "";
		if(null != uRoles && uRoles.size()>0){
			roleId = uRoles.get(0).getRoleId();
		}
		List<SysRole> roles = sysRoleService.selectByExample(null);
		model.addAttribute("roles", roles);
		model.addAttribute("roleId", roleId);
		return "/Admin/edit";
	}

	@RequestMapping("/admin/edit")
	@RequiresRoles(value = { "admin" })
	@RequiresAuthentication
	@SystemControllerLog(description = "编辑后台用户")
	public String edit(Model model, @ModelAttribute("admin") Admin admin) {
		adminService.updateAdmin(admin);
		String id = admin.getId();
		List<SysAdminRole> uroles = getURoles(id);
		delURoles(uroles);
		String roleId = admin.getRoleId();
		addURole(id, roleId);
		return redirect;
	}
	@RequestMapping("/admin/resetpwd")
	@RequiresRoles(value = { "admin" })
	@RequiresAuthentication
	@SystemControllerLog(description = "重置密码")
	public String resetpwd(Model model, @RequestParam(value = "id", required = true) String id) {
		Admin admin = new Admin();
		admin.setId(id);
		admin.setPassword(Md5Util.Md5("111111"));
		adminService.updateAdmin(admin);
		return redirect;
	}

	private void addURole(String id, String roleId) {
		if(StringUtils.isNotEmpty(roleId)){
			SysAdminRole urole = new SysAdminRole();
			urole.setId(IdGen.randomBase62(5));
			urole.setUserId(id);
			urole.setRoleId(roleId);
			sysAdminRoleService.addSysAdminRole(urole);
		}
	}

	private void delURoles(List<SysAdminRole> uroles) {
		for (SysAdminRole sysAdminRole : uroles) {
			String id2 = sysAdminRole.getId();
			sysAdminRoleService.delSysAdminRoleById(id2);
		}
	}

	private List<SysAdminRole> getURoles(String id) {
		SysAdminRoleExample example = new SysAdminRoleExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andUserIdEqualTo(id);
		List<SysAdminRole> uroles = sysAdminRoleService.selectByExample(example );
		return uroles;
	}

	@RequestMapping("/admin/view")
	public String view(Model model,
			@RequestParam(value = "id", required = true) String id) {
		Admin admin = adminService.getAdminById(id);
		model.addAttribute("admin", admin);
		return "/Admin/view";
	}
}
